Skip to content

feat(elements): Consider ValidityState in FieldState - #3594

Merged
LekoArts merged 5 commits into
mainfrom
lekoarts/sdk-1638-bug-client-side-field-validation-not-considered-in-field
Jun 21, 2024
Merged

feat(elements): Consider ValidityState in FieldState#3594
LekoArts merged 5 commits into
mainfrom
lekoarts/sdk-1638-bug-client-side-field-validation-not-considered-in-field

Conversation

@LekoArts

@LekoArts LekoArts commented Jun 19, 2024

Copy link
Copy Markdown
Contributor

Description

This PR adds the ValidityState (https://www.radix-ui.com/primitives/docs/components/form#validitystate) to our FieldState logic. So e.g. if someone enters an invalid email address into a <input type="email" /> and focus away, our <FieldState> will now report an error. Previously it didn't do anything.

In this PR also some data attributes were removed/renamed, see changeset + inline comments for more details.

Docs PR: clerk/clerk-docs#1179

Fixes SDK-1638

Checklist

  • npm test runs as expected.
  • npm run build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@changeset-bot

changeset-bot Bot commented Jun 19, 2024

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 76391bc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@clerk/elements Minor
@clerk/ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

* @see https://developer.mozilla.org/en-US/docs/Web/API/ValidityState
*/
const enrichFieldState = (validity: ValidityState | undefined, fieldState: FieldStates) => {
return validity?.valid === false ? FIELD_STATES.error : fieldState;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valid

A boolean value that is true if the element meets all its validation constraints, and is therefore considered to be valid, or false if it fails any constraint.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also thought about doing this instead:

return fieldState === FIELD_STATES.idle ? validity?.valid : fieldState

I guess it's a question of what should take precedence. The validity or our own fieldState

return {
props: {
[`data-${validity}`]: true,
...(errors.length > 0 ? { 'data-global-error': true } : {}),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't make much sense to me to have data-valid/data-invalid on the <form> if that's already placed further below in the DOM tree. And in those instances we refer to the <FieldError> state, not <GlobalError>.

So renaming this to make it more clear that this is about global errors.

return {
hasValue,
props: {
[`data-${validity}`]: true,

@LekoArts LekoArts Jun 19, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Radix already adds data-valid/data-invalid correctly to the field, so this is removed to not end up in a situation where both data-valid/data-invalid can exist at the same time.

Comment on lines +462 to +472
<RadixValidityState>
{validity => {
const enrichedFieldState = enrichFieldState(validity, fieldState);

return (
<ValidityStateContext.Provider value={validity}>
{typeof children === 'function' ? children(enrichedFieldState) : children}
</ValidityStateContext.Provider>
);
}}
</RadixValidityState>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main gist of this PR. Using Context here to not have to pass around values through props 👍

@LekoArts
LekoArts marked this pull request as ready for review June 19, 2024 08:38
@LekoArts
LekoArts enabled auto-merge (squash) June 21, 2024 05:24
@LekoArts
LekoArts merged commit 5aedc29 into main Jun 21, 2024
@LekoArts
LekoArts deleted the lekoarts/sdk-1638-bug-client-side-field-validation-not-considered-in-field branch June 21, 2024 05:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants